package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//修改密码
public class UserUpwd_Activity extends BaseActivity {
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.tv_input_tip)
    TextView tvInputTip;
    @BindView(R.id.edit_pwdok)
    EditText editPwdok;
    @BindView(R.id.edit_pwdqueren)
    EditText editPwdqueren;
    @BindView(R.id.btn_res)
    Button btnRes;
    @BindView(R.id.toolber_color_title)
    TextView toolberTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_uset_update_pwd);
        ButterKnife.bind(this);
        toolberTitle.setText("修改密码");
    }

    @OnClick(R.id.btn_res)
    public void onClick() {
        if (editPwd.getText().toString().trim().equals("")) {
            Toast.makeText(this, "请输入原密码！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (editPwdok.getText().toString().trim().equals("")) {
            Toast.makeText(this, "请输入新密码！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!editPwdok.getText().toString().trim().equals(editPwdqueren.getText().toString().trim())) {
            Toast.makeText(this, "两次新密码输入不一致！", Toast.LENGTH_SHORT).show();
            return;
        }
        initBtn();
    }

    private void initBtn() {
        String url = Constants.modifypassword;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("修改密码", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    if (code.equals("1")) {
                        toast("修改密码成功！");
                        finish();
                    } else {
                        toast("密码修改失败！");
                        Log.i("站好", "账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userPhone", BcUtils.getPhone());
                    json_url.put("oldPassword", editPwd.getText().toString().trim());
                    json_url.put("newPassword", editPwdok.getText().toString().trim());
                    json_url.put("userId", BcUtils.getUid());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    @OnClick(R.id.toolber_color_back)
    public void onViewClicked() {
        finish();
    }
}
